<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class CreateArticleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('article', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('主键');
            $table->string('title')->nullable()->comment('文章标题');
            $table->text('content')->nullable()->comment('正文内容');
            $table->string('thumb')->nullable()->comment('文章图片');
            $table->unsignedBigInteger('visit')->nullable()->comment('访问量');
            $table->timestamps();  //创建时间戳

            //添加外键字段
            $table->unsignedBigInteger("typeid")->nullable()->comment('分类外键');

            //添加普通索引
            $table->index('typeid', 'key_article_typeid');

            //建立外键关系
            $table->foreign('typeid', 'fk_article_typeid')
                  ->references('id')
                  ->on('article_type')
                  ->constrained()
                  ->onUpdate('no action')
                  ->onDelete('cascade');
        });

        //表注释
        DB::statement("ALTER TABLE `pre_article` comment '文章表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('article');
    }
}
